import React from 'react';
import ReactDom from 'react-dom';
import zhCN from 'antd/lib/locale/zh_CN';
import { ConfigProvider } from 'antd';
import { PropsProvider, UserSrv, AuthProvider, Utils } from 'lfun-bg-comp';
import { StyleProvider, legacyLogicalPropertiesTransformer } from '@ant-design/cssinjs';
import Router from './router';
import 'antd/dist/reset.css';
import './index.less';
import './style.css';
import '@/interceptor';
import { MenuParams, getMenu } from '@/service';
import { hasPathRoot, getEduType, getUnitType, getPathRoot } from './utils';

const App = ({ user }) => {
  return (
    <ConfigProvider locale={zhCN}>
      <PropsProvider>
        <AuthProvider user={user} logoutUrl="/school/v1/system/login-out">
          <Router />
        </AuthProvider>
      </PropsProvider>
    </ConfigProvider>
  );
};

async function getInfo(user) {
  if (window.location.pathname === null) return;
  if (!hasPathRoot(window.location.pathname)) return;
  //if (!user) return;
  const patharr = window.location.pathname.toLocaleLowerCase().split('/');
  //const unit_type: any = patharr[currentIdx++];
  const unit_type = getUnitType(patharr);
  const currentIdx = unit_type === 'school' ? 2 : 3;
  let code = patharr[2];
  const system_type: any = patharr[currentIdx];
  let edu_type: any = patharr[currentIdx];

  if (unit_type === 'city') {
    edu_type = patharr[4];
  } else if (unit_type === 'county') {
    edu_type = patharr[4];
  } else if (unit_type === 'school') {
    code = patharr[1];
    // school_id = code;
    Utils.updateProps({
      schoolInfo: { school_id: code },
    });
  } else if (unit_type === 'planning_school') {
    //
  }
  if (unit_type === 'school' || unit_type === 'planning_school') {
    edu_type = getEduType(code);
  }
  const params: MenuParams = {
    unit_type,
    system_type,
    edu_type,
    user_name: encodeURIComponent(user?.user_nickname),
  };
  Utils.updateProps({
    'x-tenant-code': code,
  });
  // const { data } = await getMenu(params);

  // if (getPathRoot() === location.pathname && data.menu && data.menu.length) {
  //   const redriect = list => {
  //     if (list.length && list[0].power_url) {
  //       location.href = getPathRoot(list[0].power_url);
  //     } else {
  //       redriect(list[0].children);
  //     }
  //   };
  //   redriect(data.menu);
  // }

  const data = {
    menu: [
      {
        created_at: '2024-09-28T17:58:38.012104',
        id: '35',
        power_name: '中小学信息管理',
        power_url: '/course1',
        resource_code: '',
        action: '',
        power_type: 'menu',
        power_code: 'course1',
        parent_id: '11',
        permission_id: '0',
        sort_order: 0,
        children: [],
      },
    ],
  };
  // console.log(data)

  data.menu[0].children.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: '15',
    power_name: '单位信息(市)',
    power_url: '/course112',
    resource_code: '',
    action: '',
    power_type: 'menu',
    power_code: 'course112',
    parent_id: '11',
    permission_id: '0',
    sort_order: 0,
    children: [],
  });
  data.menu[0].children.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: '15',
    power_name: '年级管理(校)',
    power_url: '/course5',
    resource_code: '',
    action: '',
    power_type: 'menu',
    power_code: 'course5',
    parent_id: '11',
    permission_id: '0',
    sort_order: 0,
    children: [],
  });

  data.menu[0].children.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: '15',
    power_name: '杨勇真帅',
    power_url: '/course2',
    resource_code: '',
    action: '',
    power_type: 'menu',
    power_code: 'course2',
    parent_id: '11',
    permission_id: '0',
    sort_order: 0,
    children: [],
  });

  data.menu[0].children.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: '35',
    power_name: '流程',
    power_url: '/course3',
    resource_code: '',
    action: '',
    power_type: 'menu',
    power_code: 'course3',
    parent_id: '11',
    permission_id: '0',
    sort_order: 0,
    children: [],
  });
  data.menu[0].children.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: '35',
    power_name: '年级管理',
    power_url: '/course8',
    resource_code: '',
    action: '',
    power_type: 'menu',
    power_code: 'course8',
    parent_id: '11',
    permission_id: '0',
    sort_order: 0,
    children: [],
  });
  data.menu.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: 60,
    power_name: '资产管理',
    power_url: '/course9',
    resource_code: '',
    action: '',
    power_type: 'root',
    power_code: 'course9',
    parent_id: '0',
    permission_id: 0,
    sort_order: 0,
    children: [],
  });

  data.menu.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: 29,
    power_name: '制证(中职)',
    power_url: '',
    resource_code: '',
    action: '',
    power_type: 'root',
    power_code: 'config1',
    parent_id: '0',
    permission_id: 0,
    sort_order: 0,
    children: [],
  });
  data.menu[2].children.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: '35',
    power_name: '制证批次',
    power_url: '/course1',
    resource_code: '',
    action: '',
    power_type: 'menu',
    power_code: 'course1',
    parent_id: '11',
    permission_id: '0',
    sort_order: 0,
    children: [],
  });
  data.menu[2].children.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: '36',
    power_name: '制证模版',
    power_url: '/course2',
    resource_code: '',
    action: '',
    power_type: 'menu',
    power_code: 'course2',
    parent_id: '11',
    permission_id: '0',
    sort_order: 0,
    children: [],
  });
  data.menu[2].children.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: '37',
    power_name: '毕业证书',
    power_url: '/course3',
    resource_code: '',
    action: '',
    power_type: 'menu',
    power_code: 'course3',
    parent_id: '11',
    permission_id: '0',
    sort_order: 0,
    children: [],
  });
  data.menu[2].children.push({
    created_at: '2024-09-28T17:58:38.012104',
    id: '38',
    power_name: '制证公司',
    power_url: '/course4',
    resource_code: '',
    action: '',
    power_type: 'menu',
    power_code: 'course4',
    parent_id: '11',
    permission_id: '0',
    sort_order: 0,
    children: [],
  });

  Utils.updateProps({
    app: data,
    extendParams: JSON.stringify(params),
    isSchool: unit_type === 'school',
    isCity: unit_type === 'city',
    isCounty: unit_type === 'county',
    edu_type,
    account_id: user?.account_id,
    phone: user?.phone,
    user_name: user?.user_nickname,
  });
}

async function initRender() {
  if (process.env.NODE_ENV === 'mock') {
    await import('./mock/index');
  }
  Utils.updateProps({
    getTokenUrl: '/school/auth/callback',
    allowCacheRules: [/\/school\/v1\/enums\/page/gi],
  });

  const user = UserSrv.getUserByToken();
  await getInfo(user);

  ReactDom.render(
    <StyleProvider hashPriority="high" transformers={[legacyLogicalPropertiesTransformer]}>
      <App user={user} />
    </StyleProvider>,
    document.getElementById('root')
  );
}

initRender();
